import modelExtend from 'dva-model-extend'
import api from 'api'
import { pageModel } from 'utils/model'

const { pathToRegexp } = require("path-to-regexp")
const { queryExampleDetails,queryExampleCategoryTree,queryExampleOptionOptions,queryDictIntOptions } = api

export default modelExtend(pageModel, {
  namespace: 'exampleDetail',

  state: {
    data: {},
    options: {
      optionId: {
      },
      applyStatus: {
           '0': '未审批',
           '1': '已审批',
      },
      status: {
           '0': '禁用',
           '1': '活跃',
      },
      dictInt: {
      },
    },
    optionValueLabel: {
      optionId: [
      ],
      applyStatus: [
           {value: 0, label:'未审批'},
           {value: 1, label:'已审批'},
      ],
      status: [
           {value: 0, label:'禁用'},
           {value: 1, label:'活跃'},
      ],
      dictInt: [
      ],
    },
    trees: {
           categoryId: [],
     }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        const match = pathToRegexp('/example/:id').exec(pathname)
        if (match) {
          dispatch({ type: 'query', payload: { id: match[1] } })

          dispatch({
            type: 'queryExampleCategoryTree',
            payload: {},
          })

          dispatch({
            type: 'queryExampleOptionOptions',
            payload: {},
          })
          dispatch({
            type: 'queryDictIntOptions',
            payload: {},
          })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {
      const data = yield call(queryExampleDetails, payload)
      const { success, message, status, ...other } = data
      if (success) {
        yield put({
          type: 'querySuccess',
          payload: {
            data: other.payload,
          },
        })
      } else {
        throw data
      }
    },
   *queryExampleCategoryTree({payload}, {call, put}) {
      const data = yield call(queryExampleCategoryTree, payload);
      if(data.success) {
        yield put({
          type: 'querySuccessTree',
          payload: {
            categoryId: data.payload,
          }
        })
      } else {
        throw data;
      }
   },
    *queryExampleOptionOptions({ payload }, { call, put }) {
      const data = yield call(queryExampleOptionOptions, payload)
      if (data.success) {
        yield put({
          type: 'queryOptionValueLabel',
          payload: {
              optionId: data.payload
          },
        })
      } else {
        throw data
      }
    },
    *queryDictIntOptions({ payload }, { call, put }) {
      const data = yield call(queryDictIntOptions, payload)
      if (data.success) {
        yield put({
          type: 'queryOptionValueLabel',
          payload: {
              dictInt: data.payload
          },
        })
      } else {
        throw data
      }
    },
  },

  reducers: {
    querySuccess(state, { payload }) {
      const { data } = payload
      return {
        ...state,
        data,
      }
    },
  },
})
